﻿using ParadoxNotion.Design;
using UnityEngine;
using Yoozoo.Gameplay.Liberty.AI.Utils;

namespace Yoozoo.Gameplay.Liberty.AI.Node
{
    [Category("Ggame/Liberty")]
    public class NpcPathAvoidPlayer : AIBaseNode
    {
        public enum PlayerLocation
        {
            None = 0,
            Left = 1,
            Right = 2,
        }
        protected AIData aiData;
        // 玩家相对于Npc的偏移量
        private PlayerLocation _playerLocation = PlayerLocation.None;
        private int _locationChangeCount;

        private LibertyPathManager _pathManager;
        private Transform _playerTrans;

        private Vector3 _lastPlayerPos;
        
        protected override void OnExecute()
        {
            aiData = _entity.data;
            
            _locationChangeCount = 0;

            _pathManager = AIGraphUtils.TrafficManager.PathManager;
            _playerTrans = LibertyAIUtils.PlayerController.playerModel;
            _lastPlayerPos = _playerTrans.position;
            
            if (Check())
            {
                RePathFind();
            }

        }


        protected override void OnUpdate()
        {

            base.SetTransformData();

            if (Vector3.Distance(_lastPlayerPos,_playerTrans.position)>0.2f)
            {
                _lastPlayerPos = _playerTrans.position;
                if (Check())
                {
                    RePathFind();
                }
            }
            

            if (_locationChangeCount > 3)
            {
                // 可以判断出玩家一直在移动阻挡Npc，Npc可以选择静止等待几秒。
            }

            if ((_entity.data.npcStatus & NpcStatus.PlayerControl) <= 0)
            {
                EndAction();
            }
        }


        private void RePathFind()
        {
            var point = _pathManager.GetPathPoint(_entity.data.pathId, _entity.data.pathPointIndex);
                
            Vector3 offset = Vector3.zero;
            float offsetFactor = 1;
            if (_playerLocation == PlayerLocation.Right)
            {
                // 偏右
                offset = offsetFactor * point.right.normalized * point.width/2;
                offset.y = 0;
            }
            else
            {
                // 偏左
                offset = -offsetFactor * point.right.normalized * point.width/2;
                offset.y = 0;
            }
                    
            Vector3 destination = point.position + offset;
            base.SetDestination(destination);
            base.SetMaxSpeed(aiData.maxSpeed);
        }
        

        private bool Check()
        {
            var pathPoint = _pathManager.GetPathPoint(_entity.data.pathId, _entity.data.pathPointIndex);
            var nextPoint =  AIGraphUtils.GetNextPoint(aiData.pathId, aiData.pathPointIndex, aiData.pathDirection);

            Vector3 aiPathDirection = (nextPoint.position - _playerTrans.position).normalized;

            Vector3 ai2PlayerVec3 = (_playerTrans.position - aiData.position).normalized;

            if (Vector3.Angle(aiPathDirection,ai2PlayerVec3) < 90f)
            {
                PlayerLocation l = PlayerLocation.None;
                // 主角在AI前方
                var closetPoint = _pathManager.GetClosestPathPoints(aiData.pathId, _playerTrans.position);

                Vector3 v1 = (_playerTrans.position - closetPoint.position).normalized;
                float angle = Vector3.Angle(closetPoint.right, v1);
                if (angle > 90)
                {
                    l = PlayerLocation.Right;
                }
                else
                {
                    l = PlayerLocation.Left;
                }

                if (l != _playerLocation)
                {
                    _locationChangeCount++;
                    _playerLocation = l;
                    return true;
                }
            }

            return false;
        }
        
        
    }
}